package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.param.CategoryUpdateInfoParam;
import cn.tedu.csmall.product.pojo.param.CategoryAddNewParam;
import cn.tedu.csmall.product.service.CategoryService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Validated
@Slf4j
@RestController
@RequestMapping("/category")
//配置模块名称
@Api(tags = "07.类别管理模块")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 1)
    @PostMapping("/add-new")
    public JsonResult addNew(@Valid CategoryAddNewParam categoryAddNewParam) {
        log.debug("开始处理【添加类别】的请求，参数：{}", categoryAddNewParam);
        categoryService.addNew(categoryAddNewParam);
        log.debug("处理【添加类别】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("根据ID删除类别")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1, message = "根据ID删除类别失败，请提交合法的ID值！")
                         @RequestParam Long categoryId)  {
        log.debug("idididiid{}",categoryId);
        categoryService.delete(categoryId);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改类别")
    @ApiOperationSupport(order = 3)
    public JsonResult update(@Valid CategoryUpdateInfoParam categoryUpdateInfoParam,
                             @Range(min = 1, message = "根据ID修改类别失败，请提交合法的ID值！")
                             @RequestParam Long id
    ) {
        log.debug("开始处理【修改类别】的请求，参数：{}", categoryUpdateInfoParam);
        categoryService.updateById(id, categoryUpdateInfoParam);
        log.debug("处理【修改类别】的请求，完成！");
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("查询类别列表")
    @ApiOperationSupport(order = 34)
    public String list() {
        throw new RuntimeException("查询出错了，导致了RuntimeException！");
    }
}
